package cn.kxm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.kxm.pojo.Company;
import cn.kxm.pojo.OfferBillDetail;
import cn.kxm.pojo.OfferPriceBill;
import cn.kxm.service.CompanyService;
import cn.kxm.service.OfferBillDetailService;
import cn.kxm.service.OfferPriceBillService;

@Controller
@RequestMapping("offerPriceBill")
public class OfferPriceBillController extends BaseController{
	@Autowired
	private OfferPriceBillService opbs;
	@Autowired
	private OfferBillDetailService obds;
	@Autowired
	private CompanyService cs;
	
	
	@RequestMapping("getAllByCondition")
	@ResponseBody
	public Object getAllByCondition(
			@RequestParam(value = "findcode", required = false)String findcode
			,@RequestParam(value = "findstartDate", required = false)String findstartDate
			,@RequestParam(value = "findendDate", required = false)String findendDate
			,@RequestParam(value = "findsupplierName", required = false)String findsupplierName
			,@RequestParam(value = "page", required = false)Integer page
			,@RequestParam(value = "rows", required = false)Integer rows){
		Date startDate=null,endDate=null;
		try {
			if(findendDate!=null&&!findstartDate.equals("")){
				startDate = new SimpleDateFormat("yyyy-MM-dd").parse(findstartDate);
			}
			if(findendDate!=null&&!findendDate.equals(""))
			endDate=new SimpleDateFormat("yyyy-MM-dd").parse(findendDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", opbs.getCountByCondition(findcode, startDate, endDate, findsupplierName));
		List<OfferPriceBill> list=opbs.getAllByCondition(findcode, startDate, endDate, findsupplierName,(page-1)*rows,rows);
		map.put("rows", list);
		return JSON.toJSONString(map);
	}
	
	
	@RequestMapping("getOfferPriceBillDetail")
	@ResponseBody
	public String getOfferPriceBillDetail(@RequestParam(value="code",required=false)String code){
		List<Map<String, String>> list=obds.findDetailByBillCode(code);
		return JSON.toJSONString(list);
	}
	
	
	
	@RequestMapping("toChangeQuotationDocument")
	public String toChangeQuotationDocument(@RequestParam(value="code",required=false)String code,Model model){
		OfferPriceBill opb=opbs.getOfferPriceBill(code);
		Company company=cs.findAll(null,opb.getCustomName(),null,0,1).get(0);
		
		model.addAttribute("code", code);
		model.addAttribute("addDate", opb.getOfferTime());
		model.addAttribute("contacter", opb.getLinkname());
		model.addAttribute("supplierName", opb.getCustomName());
		model.addAttribute("phone", opb.getPhone());
		model.addAttribute("memo",opb.getMemo());
		
		model.addAttribute("fax", company.getFax());
		List<Map<String, String>> list=obds.findDetailByBillCode(code);
		model.addAttribute("list", list);
		if(opb.getCheckStatus().equals("已审核")){
			return "web/goods/showQuotationDocument";
		}else {
			return "web/goods/changeQuotationDocument";
		}
	}
	//添加新的报价单
	@SuppressWarnings("unchecked")
	@RequestMapping("saveOfferBill")
	@ResponseBody
	public String saveOfferBill(String code,
			String customName,
			String linkman,
			String phone,
			String memo,
			Double money,
			String offerTime,
			String data,
			Integer total) {
		OfferPriceBill opb=null;
		try {
			opb=new OfferPriceBill(code, customName, linkman, phone, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(offerTime), money, memo, "1");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<Map<String, Object>> oldlist=(List<Map<String, Object>>) JSON.parse(data);
		oldlist.remove(oldlist.size()-1);
		List<OfferBillDetail> list=new ArrayList<OfferBillDetail>();
		for (Map<String, Object> map : oldlist) {
			OfferBillDetail offerBillDetail=new OfferBillDetail(0, opb.getCode(), 
					map.get("code").toString(),
					Integer.parseInt(map.get("amount").toString()), 
					Double.parseDouble(map.get("price").toString()), 
					map.get("cycle").toString(), 
					map.get("memo").toString());
			list.add(offerBillDetail);
		}
		Map<String, String> map=new HashMap<String, String>();
		if (opbs.insertOfferPriceBill(opb, list)) {
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("changeOfferBill")
	@ResponseBody
	public String changeOfferBill(String code,
			String customName,
			String linkman,
			String phone,
			String memo,
			Double money,
			String offerTime,
			String data,
			Integer total) {
		OfferPriceBill opb=null;
		try {
			opb=new OfferPriceBill(code, customName, linkman, phone, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(offerTime), money, memo, "1");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<Map<String, Object>> oldlist=(List<Map<String, Object>>) JSON.parse(data);
		oldlist.remove(oldlist.size()-1);
		List<OfferBillDetail> list=new ArrayList<OfferBillDetail>();
		for (Map<String, Object> map : oldlist) {
			OfferBillDetail offerBillDetail=new OfferBillDetail(0, opb.getCode(), 
					map.get("code").toString(),
					Integer.parseInt(map.get("amount").toString()), 
					Double.parseDouble(map.get("price").toString()), 
					map.get("cycle").toString(), 
					map.get("memo").toString());
			list.add(offerBillDetail);
		}
		Map<String, String> map=new HashMap<String, String>();
		if (opbs.updateOfferPriceBill(opb, list)) {
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	
	@RequestMapping("deleteOfferPriceBills")
	@ResponseBody
	public String deleteOfferPriceBills(String codes){
		@SuppressWarnings("unchecked")
		List<String> list=(List<String>) JSON.parse(codes);
		
		Map<String, String> map=new HashMap<String, String>();
		
		for (String code : list) {
			if (opbs.deleteOfferPriceBillAndDetails(code)) {
				map.put("result", "success");
			}else{
				map.put("result", "error");
			}

		}
		return JSON.toJSONString(map);
	}
	
	@RequestMapping("audit")
	@ResponseBody
	public String audit(String code){
		Map<String, String> map=new HashMap<String, String>();
		if(opbs.audit(code)){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	
	@RequestMapping("revoke")
	@ResponseBody
	public String revoke(String code){
		System.out.println("aaa");
		Map<String, String> map=new HashMap<String, String>();
		if(opbs.revoke(code)){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}
	
}
